package com.hanhai.zrb.mobile.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hanhai.common.annotation.LoginNeedless;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.biz.service.CommonCounterService;
import com.hanhai.zrb.api.biz.service.UserStatisticDetailService;
import com.hanhai.zrb.api.client.facade.UserStatisticDetailFacade;
import com.hanhai.zrb.api.client.result.CollectionResult;
import com.hanhai.zrb.api.util.DateUtil;
import com.hanhai.zrb.mobile.result.BaseApiResult;
import com.hanhai.zrb.mobile.util.FrontLoginUtil;
import com.hanhai.zrb.model.common.BaseUser;
import com.hanhai.zrb.model.common.CommonCounterKey;
import com.hanhai.zrb.model.user.UserStatisticDetail;
import com.hanhai.zrb.model.usercenter.CommonStatistic;
import com.hanhai.zrb.model.usercenter.IntentionStatistic;
import com.hanhai.zrb.model.usercenter.MyList;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;


/**
 * @author lixiong
 *
 */
@RequestMapping("/userStatisticDetail")
@RestController
public class UserStatisticDetailApiController extends BaseController{
	
	@Resource
	private UserStatisticDetailFacade userStatisticDetailFacade;
	
	@Resource
	private UserStatisticDetailService userStatisticDetailService;
	
	@Resource
	private CommonCounterService commonConterService;
	
	
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "收藏",  response = BaseApiResult.class, notes = "收藏")
	@RequestMapping("/doCollectionAdd")
	public BaseApiResult doAdd(Integer type,Long id){
		BaseApiResult result = new BaseApiResult();
		boolean bool =checkTypeCanCollection(type);
		if(!bool){
			result.setRes(0);
			result.setMsg("收藏类型不支持");
			return result;
		}
		
		BaseUser user = FrontLoginUtil.getCurrentUser();
		UserStatisticDetail userStatisticDetail = new UserStatisticDetail();
		userStatisticDetail.setDestId(id);
		userStatisticDetail.setType(type);
		userStatisticDetail.setUserId(user.getUid());
		CollectionResult  collectionResult = userStatisticDetailFacade.doCollectionAdd(userStatisticDetail);
		result.setData(collectionResult.isSuccess());
		return result;
	}
	
	private boolean checkTypeCanCollection(Integer type){
		UserStatisticDetail.Type statisticTypc = UserStatisticDetail.Type.getValue(type);
		if(statisticTypc !=null ){
			if(statisticTypc == UserStatisticDetail.Type.fund_col
					||statisticTypc == UserStatisticDetail.Type.pro_col){
				return true;
			}
		}
		return false;
		
	}
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "取消收藏",  response = BaseApiResult.class, notes = "取消收藏")
	@RequestMapping("/doCollectionCancel")
	public BaseApiResult doCancel(Integer type,Long id){
		BaseApiResult result = new BaseApiResult();
		boolean bool =checkTypeCanCollection(type);
		if(!bool){
			result.setRes(0);
			result.setMsg("收藏类型不支持");
			return result;
		}
		BaseUser user = FrontLoginUtil.getCurrentUser();
		UserStatisticDetail userStatisticDetail = new UserStatisticDetail();
		userStatisticDetail.setDestId(id);
		userStatisticDetail.setType(type);
		userStatisticDetail.setUserId(user.getUid());
		CollectionResult  collectionResult = userStatisticDetailFacade.doCollectionCancel(userStatisticDetail);
		result.setData(collectionResult.isSuccess());
		return result;
	}
	
	@RequestMapping("/isCollected")
	public BaseApiResult isCollected(Long id,Integer type){
		BaseApiResult result = new BaseApiResult();
		boolean bool =checkTypeCanCollection(type);
		if(!bool){
			result.setRes(0);
			result.setMsg("收藏类型不支持");
			return result;
		}
		BaseUser loginUser = FrontLoginUtil.getCurrentUser();
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		if(null!=loginUser){
			map.put("isCollected", userStatisticDetailService.isCollected(loginUser.getUid(), id, type));
		}else{
			map.put("isCollected", false);
		}
		result.setData(map);
		return result;
	}
	
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "查询我关注的项目列表",  response = BaseApiResult.class, notes = "查询我关注的项目列表")
	@RequestMapping(value="/getCollectProjects")
	public BaseApiResult getCollectProjects(HttpServletRequest request,HttpServletResponse response, Long createTime, Integer limit){
	    BaseApiResult result = new BaseApiResult();
	    BaseUser baseUser = FrontLoginUtil.getCurrentUser();
        if(null!=baseUser){
        	Long uid = baseUser.getUid();
        	Date date = (null==createTime?null:DateUtil.getStringToDate(DateUtil.FULL_ST_FORMAT,DateUtil.getDate(createTime)));
        	List<CommonStatistic> list = userStatisticDetailFacade.getUserCollectionProjects(uid, date, limit);
        	result.setData(list);
			result.setRes(1);
			logger.info("user query collected project successed,user id is:"+uid);
        }else{
        	result.setMsg("会话超时，请重新登录客户端");
        	result.setRes(0);
        	logger.info("user query collected project failed");
        }
		return result;
	}
	
	
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "查询我关注的资金列表",  response = BaseApiResult.class, notes = "查询我关注的资金列表")
	@RequestMapping(value="/getCollectFunds")
	public BaseApiResult getCollectFunds(HttpServletRequest request,HttpServletResponse response, Long createTime, Integer limit){
	    BaseApiResult result = new BaseApiResult();
	    BaseUser baseUser = FrontLoginUtil.getCurrentUser();
        if(null!=baseUser){
        	Long uid = baseUser.getUid();
        	Date date = (null==createTime?null:DateUtil.getStringToDate(DateUtil.FULL_ST_FORMAT,DateUtil.getDate(createTime)));
        	List<CommonStatistic> list = userStatisticDetailFacade.getUserCollectionFund(uid, date, limit);
        	result.setData(list);
			result.setRes(1);
			logger.info("user query collected fund successed,user id is:"+uid);
        }else{ 
        	result.setMsg("会话超时，请重新登录客户端");
        	result.setRes(0);
        	logger.info("user query collected fund failed");
        }
		return result;
	}
	
	//已废弃的接口
	@Deprecated
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "查询我关注的发现列表",  response = BaseApiResult.class, notes = "查询我关注的发现列表")
	@RequestMapping(value="/getCollectIntentions")
	public BaseApiResult getCollectIntentions(HttpServletRequest request,HttpServletResponse response, Long createTime, Integer limit){
	    BaseApiResult result = new BaseApiResult();
	    BaseUser baseUser = FrontLoginUtil.getCurrentUser();
        if(null!=baseUser){
        	Long uid = baseUser.getUid();
        	Date date = (null==createTime?null:DateUtil.getStringToDate(DateUtil.FULL_ST_FORMAT,DateUtil.getDate(createTime)));
        	List<IntentionStatistic> list = userStatisticDetailFacade.getUserCollectionIntention(uid, date, limit);
        	result.setData(list);
			result.setRes(1);
			logger.info("user query collected intention successed,user id is:"+uid);
        }else{
        	result.setMsg("会话超时，请重新登录客户端");
        	result.setRes(0);
        	logger.info("user query collected intention failed");
        }
		return result;
	}
	
	
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "查询我的项目列表",  response = BaseApiResult.class, notes = "查询我的项目列表")
	@RequestMapping(value="/getMyProjects")
	public BaseApiResult getMyProjects(HttpServletRequest request,HttpServletResponse response, Long pushTime, Integer limit){
	    BaseApiResult result = new BaseApiResult();
	    BaseUser baseUser = FrontLoginUtil.getCurrentUser();
        if(null!=baseUser){
        	Long uid = baseUser.getUid();
        	Date date = (null==pushTime?null:DateUtil.getStringToDate(DateUtil.FULL_ST_FORMAT,DateUtil.getDate(pushTime)));
        	List<MyList> list = userStatisticDetailFacade.getMyProjects(uid,date,limit);
        	result.setData(list);
			result.setRes(1);
			logger.info("user query project successed,user id is:"+uid);
        }else{
        	result.setMsg("会话超时，请重新登录客户端");
        	result.setRes(0);
        	logger.info("user query project failed");
        }
		return result;
	}
	
	
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "查询我的资金列表",  response = BaseApiResult.class, notes = "查询我的资金列表")
	@RequestMapping(value="/getMyFunds")
	public BaseApiResult getMyFunds(HttpServletRequest request,HttpServletResponse response, Long pushTime, Integer limit){
	    BaseApiResult result = new BaseApiResult();
	    BaseUser baseUser = FrontLoginUtil.getCurrentUser();
        if(null!=baseUser){
        	Long uid = baseUser.getUid();
        	Date date = (null==pushTime?null:DateUtil.getStringToDate(DateUtil.FULL_ST_FORMAT,DateUtil.getDate(pushTime)));
        	List<MyList> list = userStatisticDetailFacade.getMyFunds(uid,date,limit);
        	result.setData(list);
			result.setRes(1);
			logger.info("user query funds successed,user id is:"+uid);
        }else{
        	result.setMsg("会话超时，请重新登录客户端");
        	result.setRes(0);
        	logger.info("user query funds failed");
        }
		return result;
	}
	
	/**
	 * @废弃的接口
	 */
	@Deprecated
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "查询我的登录界面统计数据",  response = BaseApiResult.class, notes = "查询我的登录界面统计数据")
	@RequestMapping(value="/getStatisticNumber")
	public BaseApiResult getStatisticNumber(HttpServletRequest request,HttpServletResponse response){
	    BaseApiResult result = new BaseApiResult();
	    BaseUser baseUser = FrontLoginUtil.getCurrentUser();
        if(null!=baseUser){
        	Map<String, Object> statisticData = userStatisticDetailFacade.getStatisticMap(baseUser.getUid());
        	result.setData(statisticData);
			result.setRes(1);
			logger.info("get Statistic Number successed,user id is:"+baseUser.getUid());
        }else{
        	result.setMsg("会话超时，请重新登录客户端");
        	result.setRes(0);
        	logger.info("get Statistic Number failed");
        }
		return result;
	}
	
	/**
	 * 咨询动作统计接口
	 * @param request
	 * @param response
	 * @param type
	 * @param id
	 * @return
	 */
	@LoginNeedless
	@RequestMapping(value="/actionAdvisoryStatistics")
	public BaseApiResult actionAdvisoryStatistics(HttpServletRequest request,HttpServletResponse response,Integer type,Long id){
		BaseApiResult result = new BaseApiResult();
		boolean bool = false;
		
		UserStatisticDetail.Type statisticTypc = UserStatisticDetail.Type.getValue(type);
		if(statisticTypc !=null ){
			if(statisticTypc == UserStatisticDetail.Type.pro_advisory
					||statisticTypc == UserStatisticDetail.Type.fund_advisory){
				bool =  true;
			}
		}
		if(!bool){
			result.setRes(0);
			result.setMsg("咨询统计类型不支持");
			return result;
		}
		
		CommonCounterKey commonCounterKey = new CommonCounterKey();
		commonCounterKey.setMainKey(statisticTypc.getCountKey());
		commonCounterKey.setSecondaryKey(String.valueOf(id));
		commonConterService.addOne(commonCounterKey);
		return result;
	}
	
	/**
	 * 分享动作统计接口
	 * 
	 * @param request
	 * @param response
	 * @param type
	 * @param id
	 * @return
	 */
	@LoginNeedless
	@RequestMapping(value="/actionShareStatistics")
	public BaseApiResult actionShareStatistics(HttpServletRequest request,HttpServletResponse response,Integer type,Long id){
		BaseApiResult result = new BaseApiResult();
		boolean bool = false;
		
		UserStatisticDetail.Type statisticTypc = UserStatisticDetail.Type.getValue(type);
		if(statisticTypc !=null ){
			if(statisticTypc == UserStatisticDetail.Type.pro_share
					||statisticTypc == UserStatisticDetail.Type.fund_share){
				bool =  true;
			}
		}
		if(!bool){
			result.setRes(0);
			result.setMsg("分享统计类型不支持");
			return result;
		}
		

		CommonCounterKey commonCounterKey = new CommonCounterKey();
		commonCounterKey.setMainKey(statisticTypc.getCountKey());
		commonCounterKey.setSecondaryKey(String.valueOf(id));
		commonConterService.addOne(commonCounterKey);
		return result;
	}
	
}
